package com.ruoyi.course.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.course.domain.LwControls;
import com.ruoyi.course.domain.LwDetection;
import com.ruoyi.materials.domain.LwMaterial;
import com.ruoyi.materials.domain.LwMaterialInfo;
import com.ruoyi.materials.mapper.LwMaterialInfoMapper;
import com.ruoyi.materials.mapper.LwMaterialMapper;
import com.ruoyi.sfproduct.domain.vo.MaterialVo;
import com.ruoyi.sfproduct.mapper.LwSfproductMapper;
import com.ruoyi.warehouse.domain.LwSfpWarehouse;
import com.ruoyi.warehouse.domain.LwWarehousinginfo;
import com.ruoyi.warehouse.mapper.LwSfpWarehouseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.course.mapper.LwCourseMapper;
import com.ruoyi.course.domain.LwCourse;
import com.ruoyi.course.service.ILwCourseService;

/**
 * 生产过程管理Service业务层处理
 * 
 * @author sll
 * @date 2023-08-18
 */
@Service
public class LwCourseServiceImpl implements ILwCourseService 
{
    @Autowired
    private LwCourseMapper lwCourseMapper;

    @Autowired
    private LwSfproductMapper lwSfproductMapper;

    @Autowired
    private LwMaterialMapper lwMaterialMapper;

    @Autowired
    private LwMaterialInfoMapper lwMaterialInfoMapper;

    @Autowired
    private LwSfpWarehouseMapper lwSfpWarehouseMapper;

    /**
     * 查询生产过程管理
     * 
     * @param id 生产过程管理主键
     * @return 生产过程管理
     */
    @Override
    public LwCourse selectLwCourseById(Long id)
    {
        return lwCourseMapper.selectLwCourseById(id);
    }

    /**
     * 查询生产过程管理列表
     * 
     * @param lwCourse 生产过程管理
     * @return 生产过程管理
     */
    @Override
    public List<LwCourse> selectLwCourseList(LwCourse lwCourse)
    {
        List<LwCourse> lwCourses = lwCourseMapper.selectLwCourseList(lwCourse);
        lwCourses.forEach(lwCourse1 ->{
            lwCourseMapper.updateLwCourse(lwCourse1);
        });
        return lwCourses;
    }

    /**
     * 新增生产过程管理
     * 
     * @param lwCourse 生产过程管理
     * @return 结果
     */
    @Override
    public AjaxResult insertLwCourse(LwCourse lwCourse)
    {
        String strings = autoReduceInventory(lwCourse);
        if (!strings.equals("success")){
            return AjaxResult.error(strings);
        }else {
            lwCourse.setSolderId(0L);
            return AjaxResult.success(lwCourseMapper.insertLwCourse(lwCourse));
        }
    }

    /**
     * 修改生产过程管理
     * 
     * @param lwCourse 生产过程管理
     * @return 结果
     */
    @Override
    public int updateLwCourse(LwCourse lwCourse)
    {
        return lwCourseMapper.updateLwCourse(lwCourse);
    }

    /**
     * 批量删除生产过程管理
     * 
     * @param ids 需要删除的生产过程管理主键
     * @return 结果
     */
    @Override
    public int deleteLwCourseByIds(Long[] ids)
    {
        return lwCourseMapper.deleteLwCourseByIds(ids);
    }

    /**
     * 删除生产过程管理信息
     * 
     * @param id 生产过程管理主键
     * @return 结果
     */
    @Override
    public int deleteLwCourseById(Long id)
    {
        return lwCourseMapper.deleteLwCourseById(id);
    }

    @Override
    public int insertLwControls(LwControls lwControls) {
        lwControls.setTime(new Date());
        lwControls.setEmployeeId(SecurityUtils.getUserId());
        if (Objects.equals(lwControls.getFlow(), "1")){
            Long courseId = lwControls.getCourseId();
            LwCourse lwCourse = lwCourseMapper.selectLwCourseById(courseId);
            LwSfpWarehouse lwSfpWarehouse = lwSfpWarehouseMapper.selectLwSfpWarehouseBySfproductId(lwCourse.getSfproductId());

            lwSfpWarehouse.setQuantity(lwSfpWarehouse.getQuantity() - lwControls.getCompleted());
            lwSfpWarehouseMapper.updateLwSfpWarehouse(lwSfpWarehouse);
        }
        return lwCourseMapper.insertLwControls(lwControls);
    }

    @Override
    public List<LwControls> getControls(Long courseId, String flow) {
        return lwCourseMapper.getControls(courseId,flow);
    }

    @Override
    public List<LwWarehousinginfo> getWarehousinginfo(Long courseId, String flow) {
        return lwCourseMapper.getWarehousinginfo(courseId,flow);
    }

    @Override
    public int delDialogue(Long id) {
        return lwCourseMapper.delDialogueById(id);
    }

    @Override
    public int insertLwDetection(LwDetection lwDetection) {
        lwDetection.setTime(new Date());
        lwDetection.setUserId(SecurityUtils.getUserId());
        if (lwDetection.getDisqualificationNumber() == null){
            lwDetection.setDisqualificationNumber(0L);
        }
        if (lwDetection.getAcceptanceNumber() == null){
            lwDetection.setAcceptanceNumber(0L);
        }
        return lwCourseMapper.insertlwDetection(lwDetection);
    }

    @Override
    public List<LwDetection> getDetection(Long courseId, Long status) {
        return lwCourseMapper.getDetection(courseId,status);
    }

    @Override
    public boolean getIsInStock(Long id) {
        LwCourse lwCourse = lwCourseMapper.selectLwCourseById(id);
        Long solder = lwCourse.getSolderId();
        Long firstDetection = lwCourse.getFirstDetectionId();
        Long disqualifications = lwCourseMapper.selectDisqualifications(lwCourse.getId());
        if (firstDetection == null || disqualifications == null){
            return false;
        }
        return firstDetection + disqualifications == solder;
    }




    /**
     * 新增物料自动减库存
     * （半成品id绑定物料id）*排产数量
     * @return -1 原材料不足 0 半成品产出成功
     */
    public String autoReduceInventory(LwCourse lwCourse){
        List<String> insufficientMaterials = new ArrayList<>();
        //车间产出半成品数量
        Long workshopOutputNumber = lwCourse.getWorkshopOutputNumber();
        //半成品id
        Long sfproductId = lwCourse.getSfproductId();
        //拿到半成品id对应的所有原材料
        List<MaterialVo> materialVos = lwSfproductMapper.selectMaterialVoBySfproductId(sfproductId);

        for (MaterialVo materialVo:materialVos) {
            Long materialId = materialVo.getMaterialId();
            Long quantity = materialVo.getQuantity(); // quantity 原材料对应半成品需要的数量
            LwMaterial lwMaterial = lwMaterialMapper.selectLwMaterialByMaterialId(materialId);
            Long materialQuantity = lwMaterial.getQuantity();
            long q = materialQuantity - (quantity * workshopOutputNumber);
            if (q<0){
                insufficientMaterials.add("原材料" + lwMaterial.getMaterialNumber() + "不足" + q); // 将不足的原材料信息加入列表
            }
        }
        if (!insufficientMaterials.isEmpty()){
            return insufficientMaterials.toString();
        }


        //循环原材料list 通过materialId查询对应该原材料的materialQuantity
        for (MaterialVo materialVo:materialVos) {
            Long materialId = materialVo.getMaterialId();
            Long quantity = materialVo.getQuantity(); // quantity 原材料对应半成品需要的数量
            LwMaterial lwMaterial = lwMaterialMapper.selectLwMaterialByMaterialId(materialId);
            Long materialQuantity = lwMaterial.getQuantity();
            //原材料重新设置数量 原材料总数 - (原材料对应半成品需要的数量 * 车间产出半成品数量)
            long q = materialQuantity - (quantity * workshopOutputNumber);
            lwMaterial.setQuantity(q);
            lwMaterialMapper.updateLwMaterial(lwMaterial);

            LwMaterialInfo materialInfo = new LwMaterialInfo();
            materialInfo.setMaterialId(materialId);
            materialInfo.setTime(new Date());
            materialInfo.setEmployeeId(SecurityUtils.getUserId());
            materialInfo.setFlow(0L);
            materialInfo.setCompleted(quantity * workshopOutputNumber);
            materialInfo.setRemark("自产单号:"+lwCourse.getRemark()+"出库");
            lwMaterialInfoMapper.insertLwMaterialInfo(materialInfo);
        }
        return "success";

    }
}










